package com.cnhis.cloudhealth.clinical.osworks.commonsql.bo;

import com.cnhis.cloudhealth.clinical.osworks.commonsql.dao.OperationHocusDao;
import com.cnhis.cloudhealth.clinical.osworks.commonsql.dao.OsCommonSqlDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.cardmember.bo.CardMemberMangerBo;
import com.cnhis.cloudhealth.commons.exception.ExBaseException;
import com.cnhis.cloudhealth.commons.util.DateUtil;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.Xml2JsonUtil;
import com.cnhis.cloudhealth.commons.vo.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OperationHocusBo {

	@Resource
	private OperationHocusDao operationHocusDao;
	
	@Resource
	private OsCommonSqlDao osCommonSqlDao;
	
	@Resource
	private OsHooperBo osHooperBo;
	
	@Autowired
	private CardMemberMangerBo cardMemberMangerBo;

	/**
	 * 加载手术麻醉系统病人
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public ModelVo get16762(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			Integer syspar = DateUtil.timeRange(GlobalVariable.StringIsNull(map.get("startdate")),GlobalVariable.StringIsNull(map.get("enddate")));
			Page page = null;
			if(syspar.equals(0)){
				page = operationHocusDao.get16762(map,GlobalVariable.IntegerIsNull(map.get("page")),GlobalVariable.IntegerIsNull(map.get("rows")));
			}else{
				page = operationHocusDao.get16762_view(map,GlobalVariable.IntegerIsNull(map.get("page")),GlobalVariable.IntegerIsNull(map.get("rows")));
			}
			mv.setRows(page.getList());
			mv.setTotal(page.getTotalNumber());
			mv.setRecordsFiltered(page.getTotalNumber());
			mv.setRecordsTotal(page.getTotalNumber());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	/**
	 * 加载手术麻醉
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public ModelVo getHocusVDG1(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.getHocusVDG1(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	/**
	 * 加载手术麻醉术前诊断
	 * @param map
	 * @return
	 */
	public ModelVo get16775(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.get16775(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	@SuppressWarnings("rawtypes")
	public ModelVo get880(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			String lvalue= osCommonSqlDao.getSysParameters(100,105003,406);
			map.put("lvalue", "lvalue");
			List list = operationHocusDao.get880(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo get882(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			Page page = operationHocusDao.get882(map,GlobalVariable.IntegerIsNull(map.get("page")),GlobalVariable.IntegerIsNull(map.get("rows")));
			mv.setRows(page.getList());
			mv.setTotal(page.getTotalNumber());
			mv.setRecordsFiltered(page.getTotalNumber());
			mv.setRecordsTotal(page.getTotalNumber());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	/**
	 * 手术麻醉病人信息保存
	 * 张清华
	 * 2017-03-03 
	 * Operation_OperRecord_Entry_New2 保存手术麻醉单
	 */
	@SuppressWarnings({ "rawtypes", "unused", "unchecked" })
	public ModelVo updateOperationOperRecord(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		Map mapxml = null;
		try {
			mapxml = Xml2JsonUtil.xmlElements(map.get("Doc").toString().toLowerCase());
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new ExBaseException("解析XML文档时发生错误，错误号: %d.");
		}
		try{
			//VAT1数据
			List TmpVAT1_1 = (List) mapxml.get("vat1");
			//VAX1病人手术人员
			List TmpVAX1_1 = (List) mapxml.get("vax1");
			//VA01诊断记录
			List TmpVAO1_1 = (List) mapxml.get("vao1");
			//VAV1病人手术情况
			List TmpVAV1_1 = (List) mapxml.get("vav1");
			//获取手术ID
			String vat01 =  GlobalVariable.StringIsNull(map.get("vat01"));
			//根据手术ID获取手术信息
			List vat1 = operationHocusDao.getVat1(vat01);
			//判断手术是否存在
			if(vat1.size()<=0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("没有找到病人手术信息,请重新刷新后再试!");
				return mv;
			}
			Map vat1Map = (Map) TmpVAT1_1.get(0);
			vat1Map.put("vat01", vat01);
			int i = operationHocusDao.updateVat1(vat1Map);
			if(i<=0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("更新病人手术信息出错,请检查后再次提交!");
				return mv;
			}
			//实行手术新增
            if(TmpVAV1_1.size()>0){
                String vav01Max = operationHocusDao.selectMaxVav1();
                operationHocusDao.deleteVav1(vat1Map);
                for(int j = 0; j<TmpVAV1_1.size(); j++){
                    Map vav1map = (Map) TmpVAV1_1.get(j);
                    vav1map.put("vat01", vat01);
                    String vav01 = GlobalVariable.StringIsNull(vav1map.get("vav01"));
                    if(vav01!=null&&vav01!=""){
                        operationHocusDao.updateVav1(vav1map);
                    }else{
                        vav1map.put("vav01Max", vav01Max);
                        vav1map.put("j", j);
                        operationHocusDao.insertVav1(vav1map);
                    }
                }
            } else {
                operationHocusDao.deleteVav1(vat1Map);
            }
			int acf01 =  GlobalVariable.IntegerIsNull(((Map<String, Object>) vat1.get(0)).get("acf01"));
			if(acf01 == 1){
				operationHocusDao.deleteVao1(vat01);
				String vao1Max = operationHocusDao.selectMaxVao1();
				if(TmpVAO1_1.size()>0){
					for(int j=0;j<TmpVAO1_1.size();j++){
						Map vao1map = (Map) TmpVAO1_1.get(j);
						vao1map.put("vao1", vao1Max);
						vao1map.put("j", j);
						vao1map.put("vaa01", GlobalVariable.StringIsNull(((Map<String, Object>) vat1.get(0)).get("vaa01")));
						vao1map.put("vaa07", GlobalVariable.StringIsNull(((Map<String, Object>) vat1.get(0)).get("vaa07")));
						operationHocusDao.insertVao1(vao1map);
					}
				}
			}else{
				operationHocusDao.deleteVao2(vat01);
				String vao2Max = operationHocusDao.selectMaxVao2();
				if(TmpVAO1_1.size()>0){
					for(int j=0;j<TmpVAO1_1.size();j++){
						Map vao2map = (Map) TmpVAO1_1.get(j);
						vao2map.put("vao2", vao2Max);
						vao2map.put("j", j);
						vao2map.put("vaa01", GlobalVariable.StringIsNull(((Map<String, Object>) vat1.get(0)).get("vaa01")));
						vao2map.put("vaa07", GlobalVariable.StringIsNull(((Map<String, Object>) vat1.get(0)).get("vaa07")));
						operationHocusDao.insertVao2(vao2map);
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	/**
	 * 手术麻醉未结帐单病人信息查询
	 * 张清华
	 * 2017-03-05 
	 * 获取手术病人手术医师
	 */
	@SuppressWarnings("rawtypes")
	public ModelVo getVat1Vax1(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.getVat1Vax1(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo selectCAM1(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.selectCAM1(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo getHocusOneVDG1(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.getHocusOneVDG1(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	/**
	 * 保存术中监测
	 * @param map
	 * @return
	 */
	public ModelVo insertVdg1Vdg03(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		Map mapxml = null;
		try {
			mapxml = Xml2JsonUtil.xmlElements(map.get("Doc").toString());
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new ExBaseException("解析XML文档时发生错误，错误号: %d.");
		}
		try{
			//VDG1数据
			List TmpVDG1 = (List) mapxml.get("VDG1");
			if(TmpVDG1.size() <= 0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("没有找到需要提交的信息!");
				return mv;
			}
			Integer vdg03 = GlobalVariable.IntegerIsNull(map.get("VDG03"));
			Long vdg01 = operationHocusDao.getMaxVdg01();
			map.put("list", TmpVDG1);
			map.put("vdg01", vdg01);
			if(vdg03==1){//用药保存
				operationHocusDao.insertTmpVDG101(map);
			}else if(vdg03==2){//输液保存
				operationHocusDao.insertTmpVDG102(map);
			}else if(vdg03==3){//输血保存
				operationHocusDao.insertTmpVDG103(map);
			}else if(vdg03==5){//事件保存
				operationHocusDao.insertTmpVDG105(map);
			}else if(vdg03==6){//出量保存
				operationHocusDao.insertTmpVDG106(map);
			}else if(vdg03==7){//输氧保存
				operationHocusDao.insertTmpVDG107(map);
			}else if(vdg03==8){//监测保存
				operationHocusDao.insertTmpVDG108(map);
			}   
			
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	/**
	 * 修改术中监测
	 * @param map
	 * @return
	 */
	public ModelVo updateVdg1Vdg03(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		Map mapxml = null;
		try {
			mapxml = Xml2JsonUtil.xmlElements(map.get("Doc").toString());
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new ExBaseException("解析XML文档时发生错误，错误号: %d.");
		}
		try{
			//VDG1数据
			List TmpVDG1 = (List) mapxml.get("VDG1");
			if(TmpVDG1.size() <= 0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("没有找到需要修改的信息!");
				return mv;
			}
			Integer vdg03 = GlobalVariable.IntegerIsNull(map.get("VDG03"));
			map.put("list", TmpVDG1);
			if(vdg03==1){//用药
				operationHocusDao.updateTmpVDG101(map);
			}else if(vdg03==2){//输液
				operationHocusDao.updateTmpVDG102(map);
			}else if(vdg03==3){//输血
				operationHocusDao.updateTmpVDG103(map);
			}else if(vdg03==5){//事件
				operationHocusDao.updateTmpVDG105(map);
			}else if(vdg03==6){//出量
				operationHocusDao.updateTmpVDG106(map);
			}else if(vdg03==7){//输氧
				operationHocusDao.updateTmpVDG107(map);
			}else if(vdg03==8){//监测
				operationHocusDao.updateTmpVDG108(map);
			}else if(vdg03==9){//监测
				operationHocusDao.updateTmpVDG108(map);
			}   
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo getHocusSomeVDG1(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.getHocusSomeVDG1(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo hocudrugBby01(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			Page page = operationHocusDao.hocudrugBby01(map,GlobalVariable.IntegerIsNull(map.get("page")),GlobalVariable.IntegerIsNull(map.get("rows")));
			mv.setRows(page.getList());
			mv.setTotal(page.getTotalNumber());
			mv.setRecordsFiltered(page.getTotalNumber());
			mv.setRecordsTotal(page.getTotalNumber());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo hoBij02() {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.hoBij02();
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
	public ModelVo getHocusVDG1VDG08(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List newList = null;
			List list = new ArrayList<>();
			if(GlobalVariable.IntegerIsNull(map.get("vdg03"))==8){
				list = operationHocusDao.getHocusVDG1VDG08(map);
				List<Map<String, Object>> vdgList = new ArrayList<Map<String, Object>>();
				if(list.size() > 0){
					List listDate = operationHocusDao.getHocusVDG1VDG10(map);
					for(int i=0;i<listDate.size();i++){
						Map mapDate = (Map) listDate.get(i);
						Map vdgmap=new HashMap<>();
						vdgmap.put("vdg10", "'"+mapDate.get("vdg10")+"'");
						for(int j=0;j<list.size();j++){
							Map mapList = (Map) list.get(j);
							if(GlobalVariable.StringIsNull(mapDate.get("vdg10")).equals(GlobalVariable.StringIsNull(mapList.get("vdg10")))){
								if(!GlobalVariable.StringIsNull(mapList.get("ssy")).equals("")){
									vdgmap.put("ssy", mapList.get("ssy"));
								}
								if(!GlobalVariable.StringIsNull(mapList.get("szy")).equals("")){
									vdgmap.put("szy", mapList.get("szy"));
								}
								if(!GlobalVariable.StringIsNull(mapList.get("hx")).equals("")){
									vdgmap.put("hx", mapList.get("hx"));
								}
								if(!GlobalVariable.StringIsNull(mapList.get("mb")).equals("")){
									vdgmap.put("mb", mapList.get("mb"));
								}
								if(!GlobalVariable.StringIsNull(mapList.get("yll")).equals("")){
									vdgmap.put("yll", mapList.get("yll"));
								}
							}
						}
						vdgList.add(vdgmap);
					}
					newList = operationHocusDao.getVdg08Desc(vdgList);
				}
			}else{
				list = operationHocusDao.getHocusVDG1VDG09(map);
				List<Map<String, Object>> vdgList = new ArrayList<Map<String, Object>>();
				if(list.size() > 0){
					List listDate = operationHocusDao.getHocusVDG1VDG10(map);
					for(int i=0;i<listDate.size();i++){
						Map mapDate = (Map) listDate.get(i);
						Map vdgmap=new HashMap<>();
						vdgmap.put("vdg10", "'"+mapDate.get("vdg10")+"'");
						for(int j=0;j<list.size();j++){
							Map mapList = (Map) list.get(j);
							if(GlobalVariable.StringIsNull(mapDate.get("vdg10")).equals(GlobalVariable.StringIsNull(mapList.get("vdg10")))){
								if(!GlobalVariable.StringIsNull(mapList.get("tw")).equals("")){
									vdgmap.put("tw", mapList.get("tw"));
								}
								if(!GlobalVariable.StringIsNull(mapList.get("spo2")).equals("")){
									vdgmap.put("spo2", mapList.get("spo2"));
								}
								if(!GlobalVariable.StringIsNull(mapList.get("ie")).equals("")){
									vdgmap.put("ie", mapList.get("ie"));
								}
								if(!GlobalVariable.StringIsNull(mapList.get("co2")).equals("")){
									vdgmap.put("co2", mapList.get("co2"));
								}
								if(!GlobalVariable.StringIsNull(mapList.get("bis")).equals("")){
									vdgmap.put("bis", mapList.get("bis"));
								}
								if(!GlobalVariable.StringIsNull(mapList.get("vy")).equals("")){
									vdgmap.put("vy", mapList.get("vy"));
								}
								if(!GlobalVariable.StringIsNull(mapList.get("st")).equals("")){
									vdgmap.put("st", mapList.get("st"));
								}
								if(!GlobalVariable.StringIsNull(mapList.get("vt")).equals("")){
									vdgmap.put("vt", mapList.get("vt"));
								}
							}
						}
						vdgList.add(vdgmap);
					}
					newList = operationHocusDao.getVdg09Desc(vdgList);
				}
			}
			mv.setRows(newList);
			mv.setTotal(newList.size());
			operationHocusDao.deletetmpList();
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo delVdg1(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			Integer vdg03 = GlobalVariable.IntegerIsNull(map.get("VDG03"));
			if(vdg03==8){//监测
				operationHocusDao.delVdg08(map);
			}else if(vdg03==9){//监测
				operationHocusDao.delVdg09(map);
			}else{
				operationHocusDao.delVdg01(map);
			}
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	@SuppressWarnings({ "rawtypes", "unused" })
	public ModelVo updatevat14(Map<String, Object> map,Integer type) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.getVAT14_15(map);
			Map vatMap = (Map) list.get(0);
			if(type==1){
				if(list!=null){
					if(GlobalVariable.StringIsNull(vatMap.get("vat14")).equals(null)||GlobalVariable.StringIsNull(vatMap.get("vat14")).equals("")){
						operationHocusDao.updatevat14(map);
					}
					if(GlobalVariable.StringIsNull(vatMap.get("vat15")).equals("") && !GlobalVariable.StringIsNull(vatMap.get("vat14")).equals(GlobalVariable.StringIsNull(map.get("vat14")))){
						operationHocusDao.updatevat14(map);
					}
				}else{
					operationHocusDao.updatevat14(map);
				}
			}else{
				if(GlobalVariable.StringIsNull(vatMap.get("vat15")).equals("")||GlobalVariable.StringIsNull(vatMap.get("vat15")).equals(null)){
					operationHocusDao.updatevat14null(map);
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	@SuppressWarnings({ "rawtypes", "unused" })
	public ModelVo updatevat15(Map<String, Object> map, Integer type) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.getVAT14_15(map);
			Map vatMap = (Map) list.get(0);
			if(type==1){
				if(list!=null){
					if(GlobalVariable.StringIsNull(vatMap.get("vat15")).equals(null)||GlobalVariable.StringIsNull(vatMap.get("vat15")).equals("")){
						operationHocusDao.updatevat15(map);
					}
				}
			}else{
				operationHocusDao.updatevat15null(map);
			}
			
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	@SuppressWarnings("rawtypes")
	public ModelVo getHocusVDG1hxVdg08(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.getHocusVDG1hxVdg08(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo getHocusVDG1VDG10VDG11(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.getHocusVDG1VDG10VDG11(map);
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo get16804() {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.get16804();
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	@SuppressWarnings("rawtypes")
	public ModelVo get16805() {
		ModelVo mv = new ModelVo();
		try{
			List list = operationHocusDao.get16805();
			mv.setRows(list);
			mv.setTotal(list.size());
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	/**
	 * 手术会员卡扣费
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public ModelVo saveOsCardMemberManger(Map<String, Object> map,Integer UserID) {
		ModelVo mv = new ModelVo();
		try{
			//查找费用
			List list = osCommonSqlDao.get7257Vaj05(map);
			if(list.size() <= 0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("没有找到该单据费用明细信息,请重新刷新后再试!");
				return mv;
			}
			String VAA01 = null ;
			String VAA07 = null ;
			for (int i = 0; i < list.size(); i++) {
				Map mapVak01 = (Map) list.get(i);
                String vak01 = GlobalVariable.StringIsNull(mapVak01.get("vak01"));
                VAA01 = GlobalVariable.StringIsNull(mapVak01.get("vaa01"));
                VAA07 = GlobalVariable.StringIsNull(mapVak01.get("vaa07"));
                if (!vak01.equals("0")) {
                    mv.setResult(StaticKeys.OH_FALSE);
					mv.setResultMsg("该单据已经结过账,不能再次结账!");
					return mv;
				}
			}
			Double VAJ38 = operationHocusDao.getVAJ38(map);
			//--获得当前操作员姓名、科室
			Integer OP_DeptId = 0;
			String OP_Code = "";
			String OP_Name = "";
			Map<String,Object> bce1map = osCommonSqlDao.selectBCE1ByUserID(UserID);
			if(bce1map!=null){
				OP_DeptId = GlobalVariable.IntegerIsNull(bce1map.get("bck01"));
				OP_Code = GlobalVariable.StringIsNull(bce1map.get("bce02"));
				OP_Name = GlobalVariable.StringIsNull(bce1map.get("bce03"));
			}
			Map<String,Object> cardMap=new HashMap<String,Object>();
            cardMap.put("ACF01",1);//服务对象 1=门诊、2=住院
            cardMap.put("BCE01",UserID);//操作员id
            cardMap.put("BCK01",OP_DeptId);//科室id
            cardMap.put("BCE02",OP_Code);//操作员code
            cardMap.put("BCE03",OP_Name);//操作员姓名
            cardMap.put("AType",0);//0 = 扣费,1 = 退费
            cardMap.put("AWorkType", 4);//1=门诊记账，2=药房发药，3=医嘱记账，4=医技执行,5=挂号，6=门诊结账 , 7=住院结帐,8=简易挂号,9=手术室扣费
            cardMap.put("VAA01",VAA01);//病人ID
            cardMap.put("VAA07",VAA07);//就诊ID
            cardMap.put("VAI01",GlobalVariable.StringIsNull(map.get("VAI01")));//单据ID,如果传入当前记账ID为记账状态，则不再扣费
            cardMap.put("VAJ38",VAJ38);//基本账户扣费
            /*一卡通扣费过程 */
            mv = cardMemberMangerBo.updatehoratecardcharge(cardMap);
            if(mv.getResult().equals(StaticKeys.OH_FALSE)){
				return mv;
			}
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	/**
	 * 手术退费申请
	 * @param param
	 * @return
	 */
	public ModelVo osVby1Return(Map param) {
		ModelVo mv = new ModelVo();
		Map mapxml = null;
		try {
			mapxml = Xml2JsonUtil.xmlElements(param.get("Doc").toString().toLowerCase());
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new ExBaseException("解析XML文档时发生错误，错误号: %d.");
		}
		
		try{
			//--=============声明退药记录临时表=================================================
			List tmpVBY = null;
			tmpVBY = (List) mapxml.get("rxlist");
			//Map<String ,Object> tmpVBYmap = new HashMap<String ,Object>();
			//<Root><VBY1><Ie VAJ01="38" VAJ25="24" Remark="" IsBackFee="-1"/><Ie VAJ01="39" VAJ25="36" Remark="" IsBackFee="-1"/></VBY1>
			if(tmpVBY.size()<=0){
				//throw new BaseException("没有找到要提交的退药内容，请重新后重试！");
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("没有找到要提交的退费内容，请刷新后重试！");
				return mv;
			}
			
			Integer BCE01 = GlobalVariable.IntegerIsNull(param.get("BCE01"));
			Integer BCK01D = GlobalVariable.IntegerIsNull(param.get("BCK01D"));
			String BCE03 = GlobalVariable.StringIsNull(param.get("BCE03"));
			Integer PARTS = GlobalVariable.IntegerIsNull(param.get("PARTS"));
			if(PARTS.equals(2)){
				operationHocusDao.drug_horate_transfer_data(param);
			}
			//=========临时表=========
			//operationHocusDao.insertTmpVBYyes(tmpVBY);
			//List tmpVBYyesList=drugDao.selectTmpVBYyes();
			
			//=========临时表=========
			//operationHocusDao.insertTmpVBYno(tmpVBY);
			// tmpVBYnoList=drugDao.selectTmpVBYno();						
			
			//=========退药记录=======
			List list1 = operationHocusDao.checkTmpVBYVAJ01(tmpVBY);
			//Map<String, Object> map1=new HashMap<String, Object>();
			//map1.put("list1", list1);
			
			//===========费用明细===========
			List list3 = operationHocusDao.checkTmpVAJ01(tmpVBY);
			//Map<String, Object> map3=new HashMap<String, Object>();
			//map1.put("list3", list3);
			
			Integer VAJ01=0;
			if(list3.size()>0){
				VAJ01 = GlobalVariable.IntegerIsNull(list3.get(0));
			}
			
			if(tmpVBY.size()<=0){
				//throw new BaseException("没有找到要提交的退药内容，请重新后重试！");
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("没有找到要提交的退费内容，请刷新后重试.");
				return mv;
			}else if(list1.size() >0){
				Map map = (Map) list1.get(0);
				/*String remark =  "";
				for (int i = 0; i < list1.size(); i++) {
					Map map = (Map) list1.get(i);
					remark +="、"+ GlobalVariable.StringIsNull(map.get("vby10"));
				}*/
				
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("提交的待退费记录:"+GlobalVariable.StringIsNull(map.get("vby10"))+" 已存在，请刷新后重试.");
				return mv;
			} else if(list3.size() < 0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("费用明细id：" + VAJ01 + " 不存在.");
				return mv;
			} else {
				insertUpdateVBY1(tmpVBY, BCE01, BCK01D, BCE03, "");
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
			throw ExBaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}
	
	@SuppressWarnings("rawtypes")
	public void insertUpdateVBY1(List tmpVBY,Integer BCE01,Integer BCK01D,String BCE03 ,String VAF22) throws Exception{
		try{
			/**
			 * 
			 */
			operationHocusDao.insertVBY1(tmpVBY, BCE01, BCK01D, BCE03 ,VAF22 );
			operationHocusDao.updateVBY1(tmpVBY, BCE01, BCK01D, BCE03 ,VAF22 );
		}catch(Exception e){
			e.printStackTrace();
			throw new ExBaseException("更新病人退费申请表时出错");
		}
	}
	
	//查看最大事件ID
	public Integer selectMaxvdg08Byvdg03(Map<String, Object> map) {
		Integer vdg08 = 1;
		try {
			vdg08 = operationHocusDao.selectMaxvdg08Byvdg03(map);
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return vdg08;
	}
}
